package com.site.dao.impl.master;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.site.dao.master.CodeDao;
import com.site.dto.master.CodeBizBean;
import com.site.model.master.CodeInfo;
import com.site.util.StringUtils;
import com.site.util.base.dao.BaseDaoImpl;
import com.site.util.base.dao.HQLHelper;
import com.site.util.extremetable.QueryPage;

/**
 * Code数据访问类
 * 
 * @author Lee 2013-02-25
 */
@Repository
public class CodeDaoImpl extends BaseDaoImpl implements CodeDao {

	/**
	 * 获取所有的Code信息
	 * @return List<CodeInfo>
	 */
	public List<CodeInfo> getAllCodeInfo()
	{
		HQLHelper hqlHelper=new HQLHelper("CodeInfo","code");	
		hqlHelper.addEqual("markDelete", "N");
		String hql=hqlHelper.getHQL()+" order by code.codeName,code.orderBy desc";
		return this.findByHQL(hql,null);
	}
	/**
	 * 根据codeName和codeKey获取code对象
	 * @param codeName	code名称
	 * @param codeKey	code键值
	 * @return
	 */
	public CodeInfo getCodeInfoByCodeNameAndKey(String codeName,String codeKey){
		HQLHelper hqlHelper=new HQLHelper("CodeInfo","code");	
		hqlHelper.addEqual("markDelete", "N");
		if(!StringUtils.isEmpty(codeName)){
			hqlHelper.addEqual("codeName", codeName);
		}
		if(!StringUtils.isEmpty(codeKey)){
			hqlHelper.addEqual("codeKey", codeKey);
		}
		List<CodeInfo> polist = this.findByHQL(hqlHelper.getHQL(),null);
		if(polist!=null && polist.get(0)!=null){
			return polist.get(0);
		}
		return null;
	}
	
	public List<CodeInfo> getAllCodeInfo(QueryPage queryPage,CodeBizBean code)
	{
		String hql = "";
		HQLHelper hqlHelper=new HQLHelper("CodeInfo","code");
		hqlHelper.addEqual("markDelete", "N");
		if (code!=null) {
			if(code.getCodeName()!=null&&!code.getCodeName().isEmpty())
				hqlHelper.addEqual("codeName", code.getCodeName());
			if(!StringUtils.isEmpty(code.getRemark())&&StringUtils.isEmpty(code.getRemark1()))
				hqlHelper.addLike("remark", code.getRemark());
			else if(!StringUtils.isEmpty(code.getRemark1())&&StringUtils.isEmpty(code.getRemark()))
				hqlHelper.addLike("remark", code.getRemark1());
			else if(!StringUtils.isEmpty(code.getRemark1())&&!StringUtils.isEmpty(code.getRemark())){
				hql=hqlHelper.getHQL();
				hql += " and (remark like '%"+code.getRemark1()+"%' or remark like '%"+code.getRemark()+"%') ";
			}
		}
		if(StringUtils.isEmpty(hql))
			hql=hqlHelper.getHQL();
		hql = hql+" order by code.codeName,code.orderBy desc";
		if(queryPage!=null)
			return this.findByHQL(queryPage, hql, null);
		return this.findByHQL(hql,null);
	}
	
	public List<String> getAllCodeName() {
		String hql = "select DISTINCT code.codeName  from CodeInfo code where code.markDelete='N'";
		List<String> strList = this.findByHQL(hql,null);
		return strList;
	}
	
	/**
	 * 新建Code时判断Code名称或Code键名是否存在
	 * @param codeName
	 * @param codeKey
	 * @param codeType	1表示新建 2表示为Code新加Key
	 * @return
	 */
	public boolean isExistsCodeNameOrCodeKey(String codeName,String codeKey,Integer codeType)
	{
		String sql = "";
		if(codeType==1)
			sql = "select count(CODE_ID) from m_code where code_Name='"+codeName+"' and MARK_DELETE='N'";
		else
			sql="select count(CODE_ID) from m_code where code_Name='"+codeName+"' and code_key='"+codeKey+"' and MARK_DELETE='N'";
		int result = this.getCountBySQL(sql);
		if(result>0)
			return true;
		return false;
	}
	
	/**
	 * 修改Code时判断Code名称和Code键值是否存在
	 * @param codeName
	 * @param codeKey
	 * @param type	1表示Code名称 2表示Code键值
	 * @param codeId
	 * @return
	 */
	/*public boolean isExistsCodeNameOrOrderKey(String codeName,String codeKey,int type,int codeId)
	{
		String sql = "";
		if(type==1)
		{
			sql = "select count(CODE_ID) from M_CODE where code_Name='"+codeName+"' and  code_Name<>(select code_Name from M_CODE where code_id="+codeId+") and markDelete='N'";
		}else{
			sql = "select count(CODE_ID) from M_CODE where code_Name='"+codeName+"' and code_Key='"+codeKey+"' and  code_key<>(select code_key from M_CODE where code_id="+codeId+") and markDelete='N'";
		}
		int result = this.getCountBySQL(sql);
		if(result>0)
			return true;
		return false;
	}*/
	
	/**
	 * 根据CoId查找Code对象
	 * @param codeId
	 * @return
	 */
	public CodeInfo getCodeInfoById(Integer codeId)	{
		return (CodeInfo)this.getEntityById(CodeInfo.class, codeId);
	}
	
	/**
	 * 根据Id逻辑删除Code
	 * @param codeId
	 * @return
	 */
	public int tombstoneCode(Integer codeId) {
		String sql = "update M_CODE set MARK_DELETE='Y' where CODE_ID="+codeId;
		return this.getCountBySQL(sql);
	}
	
	/**
	 * Code排序
	 * @param codeId
	 * @param orderBy
	 * @return
	 */
	public int updateCodeOrderBy(Integer codeId,Integer orderBy)
	{
		String sql = "update M_CODE set ORDER_BY="+orderBy+" where CODE_ID="+codeId;
		return this.getCountBySQL(sql);
	}

	/**
	 * 通过codeValue查找配送单code
	 * @return
	 */
	public List<CodeInfo> findCodeByCodeValue(String value){
		HQLHelper hqlHelper = new HQLHelper("CodeInfo");
		hqlHelper.addLike("codeName", "SEND_STATUS");
		hqlHelper.addLike("codeValue", value);
		hqlHelper.addEqual("markDelete", "N");
		String hql=hqlHelper.getHQL();
		return this.findByHQL(hql,null);
	}
	
	/**
	 * 根据分组名称和键名查找Code的值
	 * @param code
	 * @param key
	 * @return
	 */
	public String getCodeNameByKey(String code, String key) {
		String sql = "select CODE_VALUE from M_CODE where CODE_NAME='"+code+"' and CODE_KEY='"+key+"'";
		List<Object[]> list = super.findBySQL(sql,null);
		if(list!=null) {
			if(list.size()>0) {
				return list.get(0).toString();
			}
		}
		return null;
	}
	
	/**
	 * 查找所有的Code的备注作为查询条件
	 * @return
	 */
	public List<String> findAllCode() {
		String sql = "select t.remark from m_code t where t.mark_delete='N' group by t.remark";
		List<Object[]> list = this.findBySQL(sql);
		List<String> remarkList = new ArrayList<String>();
		for (Object object : list) {
			if(object!=null)
				remarkList.add(object.toString());
		}
		return remarkList;
	}
	@Override
	public void save(CodeInfo po) {
		super.save(po);
	}
	@Override
	public CodeInfo getEntityById(Integer codeId) {
		return super.getEntityById(CodeInfo.class, codeId);
	}
	@Override
	public void update(CodeInfo po) {
		super.update(po);
	}
	@Override
	public void markAsDeleted(Integer obj) {
		super.markAsDeleted(new CodeInfo(), obj);
	}
}
